package com.cloud.commons.utils;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class CloudDateUtils {

    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 获得本周一与当前日期相差的天数
     *
     * @return
     */
    public static int getMondayPlus() {
        Calendar cd = Calendar.getInstance();
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) {
            return -6;
        } else {
            return 2 - dayOfWeek;
        }
    }

    /**
     * 获取当天凌晨时间戳
     * @return
     */
    public static Long getCurrentDayStartTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Long start = calendar.getTimeInMillis();
        return start;
    }

    /**
     * 获取当天23点时间戳
     * @return
     */
    public static Long getCurrentDayEndTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
        Long endTime = calendar.getTimeInMillis();
        return endTime;
    }

    /**
     * 获得当前周- 周一的日期
     *
     * @return
     */
    public static Long getCurrentMonday() {
        int mondayPlus = getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus);
        //将时、分、秒、毫秒 清零
        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        currentDate.set(Calendar.MILLISECOND, 0);
        Date monday = currentDate.getTime();
        Long preMonday = monday.getTime();

        return preMonday;
    }


    /**
     * 获得当前周- 周日  的日期
     *
     * @return
     */
    public static Long getCurrentSunday() {
        int mondayPlus = getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
        //将时、分、秒、毫秒 清零
        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        currentDate.set(Calendar.MILLISECOND, 0);
        Date sunday = currentDate.getTime();
        Long preSunday = sunday.getTime();
        return preSunday;
    }

    /**
     * 获得当前月--开始日期
     *
     * @return
     */
    public static Long getCurrentMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        //将时、分、秒、毫秒 清零
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime().getTime();
    }

    /**
     * 获得当前月--结束日期
     *
     * @return
     */
    public static Long getCurrentMonthLastDay() {
        Calendar calendar = Calendar.getInstance();
        calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        //将时、分、秒、毫秒 清零
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime().getTime();
    }

    // 日期转字符串格式
    public static String timeStamp2Date(String seconds, String format) {
        if (StringUtils.isEmpty(seconds) || StringUtils.isEmpty(format)) {

        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds)));
    }

    // 字符串转日期格式
    public static Date StrToDate(String str, String formatString) {

        if (formatString == null || formatString.isEmpty()) {
            formatString = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat format = new SimpleDateFormat(formatString);

        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取给定时间 当天0点的时间戳
     *
     * @param time
     * @return
     */
    public static Long getDayStartTime(Long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time));

        //将时、分、秒、毫秒 清零
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime().getTime();
    }

    /**
     * 获取给定时间 当天24点的时间戳
     *
     * @param time
     * @return
     */
    public static Long getDayEndTime(Long time) {
        Long startTime = getDayStartTime(time);
        Long oneDayTimeMillis = 60000 * 60 * 24L;
        return startTime + oneDayTimeMillis;
    }

    /**
     * 获取倒计时
     * @param time
     * @return
     */
    public static String getTimeCountDown(Long time){

        long nd = 1000*24*60*60;//一天的毫秒数
        long nh = 1000*60*60;//一小时的毫秒数
        long nm = 1000*60;//一分钟的毫秒数
        long ns = 1000;//一秒钟的毫秒数

        long day = time/nd;//计算差多少天
        long hour = time%nd/nh;//计算差多少小时
        long min = time%nd%nh/nm;//计算差多少分钟
        long sec = time%nd%nh%nm/ns;//计算差多少秒
        return day+"天"+(hour<10?"0"+hour:hour)+"时"+(min<10?"0"+min:min)+"分"+(sec<10?"0"+sec:sec)+"秒";
    }


    /**
     * 获取过去第几天的时间戳
     *
     * @param past
     * @return
     */
    public static Long getPastDate(int past){
       Long OneWeekTime = System.currentTimeMillis();
       if (past > 0){
            OneWeekTime = OneWeekTime - (86400000 * past);
       }else if (past < 0){
           System.out.println("参数不对");
           OneWeekTime = null;
       }

       return OneWeekTime;
    }



    /**
     * 获取当前日期是星期几
     *
     * @param dt
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }


}
